# -*- coding: utf-8 -*-
import cv2
import numpy as np
import sys
import os
if __name__ == "__main__":
    print 'Total:', len(sys.argv)
    print 'list:', str(sys.argv)

    path = os.getcwd()
    print (os.listdir(path))
    str_files = os.listdir(path)

    for pic in str_files:
        print(pic)
        str_name = pic.split('.')
        # print(len(str_name))
        # print(str_name)
        # print(str_name[1])
        # print(pic)
        if(len(str_name) != 2 or str_name[1] != "png"):
            continue

        img_path = pic
        image = cv2.imread(img_path,-1)
        img_nearest = image
        # img_nearest = cv2.resize(image, (image.shape[1]/2, image.shape[0]/2), cv2.INTER_CUBIC)
        # img_nearest = cv2.resize(image, (300, 500), cv2.INTER_CUBIC)
        img_nearest = cv2.resize(image, (360, 640), cv2.INTER_CUBIC)
        
        cv2.imwrite(str_name[0]+'_360x640.png', img_nearest)
        img_size = img_nearest.shape[:2]
        filename = str_name[0] + "360x640.bin"
        print(str_name)
        file = open(filename,"wb")

        # file.write("#include \"lvgl.h\"\n\n")

        # file.write("const uint8_t " + str_name[0] + "[] = {\n")
        # file.write("  ")
        cnt =  0
        for i in range(0,img_size[0]):
            for j in range(0,img_size[1]):
                for di in img_nearest[i,j]:
                    file.write(chr(di))
                    # file.write("%c" % (di))
                    # file.write("0x%02X," % (di))
                # cnt = cnt + 1
                # if (cnt % 10 == 0):
                #     file.write("\n  ")

        # file.write("\n};")
        # file.write("\n")
        # file.write("\n")

        # struct_name = "pic_" + str_name[0]
        # file.write("const lv_img_dsc_t " + struct_name + " = {\n")
        # file.write("    .header.always_zero = 0,\n")
        # file.write("    .header.w = " + str(img_size[1]) + ",\n")
        # file.write("    .header.h = " + str(img_size[0]) + ",\n")
        # file.write("    .data_size = " + str(img_size[0]*img_size[1]) + ",\n")
        # file.write("    .header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA,\n")
        # file.write("    .data = " + str_name[0] + ",\n")
        # file.write("}; \n")
        file.close()
        print(filename,img_size)



